﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Guru.Data;
using System.Data.SqlClient;
using Guru.Web.UI;
using Guru.Utilities.Email;

[PageTextResource("SignUp")]
public partial class SignUp : HSMPage
{
    private int _ClientID = 0;
    private int _HotelID = 0;
    private string _UserID = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private void CreateUser()
    {
        //Step1: Check duplicate username, email

        //Step2: Create user
        GuruParameterCollection parameters = new GuruParameterCollection();
        GuruParameterCollection outParams = new GuruParameterCollection();
        parameters.Add("@UserID", string.Empty, DbType.Guid, true);
        parameters.Add("@ClientID", _ClientID, DbType.Int32);
        parameters.Add("@HotelID", _HotelID, DbType.Int32);
        parameters.Add("@UserName", uxUserName.Text.Trim(), DbType.String);
        parameters.Add("@Email", uxEmail.Text.Trim(), DbType.String);
        parameters.Add("@Phone", uxPhone.Text.Trim(), DbType.String);
        parameters.Add("@FullName", uxFullName.Text.Trim(), DbType.String);
        parameters.Add("@UserPassword", Cryptor.EncryptText(uxPassword.Text.Trim()), DbType.String);
        parameters.Add("@Status", (int)HSMEnums.UserStatus.WaitForActive, DbType.Int16);
        parameters.Add("@RoleIds", (int)HSMEnums.UserType.ClientAdmin, DbType.String);
        int affectedRows = HSMCommon.HSMBusiness.UpdateData("spa_SEC_User_Insert", parameters, out outParams);
        _UserID = outParams["@UserID"].ToString();
    }

    private void CreateHotel()
    {
        string clientNameDefault = uxHotelName.Text.Trim();
        GuruParameterCollection parameters = new GuruParameterCollection();
        GuruParameterCollection outParams = new GuruParameterCollection();
        parameters.Add("@ClientID", _ClientID, DbType.Int32);
        parameters.Add("@HotelName", uxHotelName.Text.Trim(), DbType.String);
        parameters.Add("@Address", uxHotelAddress.Text.Trim(), DbType.String);
        parameters.Add("@Phone", uxHotelPhone.Text.Trim(), DbType.String);
        parameters.Add("@Fax", uxHotelFax.Text.Trim(), DbType.String);
        parameters.Add("@HotelIDNew", 0, DbType.Int32, true);

        //Get affected rows 
        int affectedRows = HSMCommon.HSMBusiness.UpdateData("spa_SEC_Hotel_Insert_Update", parameters, out outParams);
        _HotelID = outParams["@HotelIDNew"].ToInt();
    }

    private void CreateClient()
    {
        //step1: check duplicate client

        //step2: create client
        string clientNameDefault = uxHotelName.Text.Trim();
        GuruParameterCollection parameters = new GuruParameterCollection();
        GuruParameterCollection outParams = new GuruParameterCollection();
        parameters.Add("@ClientName", clientNameDefault, DbType.String);
        parameters.Add("@ClientIDNew", 0, DbType.Int32, true);

        //Get affected rows 
        int affectedRows = HSMCommon.HSMBusiness.UpdateData("spa_SEC_Client_Insert_Update", parameters, out outParams);
        _ClientID = outParams["@ClientIDNew"].ToInt();
    }

    private void SendEmailActiveAccount()
    {
        string fromEmail = HSMSessionManager.MailSettings.EmailContact;
        //read email template
        string body = HSMCommon.ReadTemplate(@"~\App_Data\EmailTemplates\ActiveAccount.htm");
        string subject = "360info - Kich Hoat Tai Khoan";
        body = body.Replace("[username]", uxUserName.Text.Trim());
        string link = HSMCommon.UrlHSM + "/CheckAuth.aspx?" + BuildStaticQueryString(string.Format("u={0}", _UserID));
        body = body.Replace("[link]", link);
        MailServices.SendEmail(fromEmail, uxEmail.Text.Trim(), subject, body, HSMSessionManager.MailSettings);
    }

    protected void uxRegister_Click(object sender, EventArgs e)
    {
        //Step1: Create client
        CreateClient();
        //Step2: Create hotel
        CreateHotel();
        //Step3: Create user
        CreateUser();
        //Step4: Send email
        SendEmailActiveAccount();
        
    }

    
}
